/**
 * @(#)HttpUtil.java, 2013-2-12. Copyright 2013 RENREN, Inc. All rights
 *                    reserved. RENREN PROPRIETARY/CONFIDENTIAL. Use is subject
 *                    to license terms.
 */
package com.wennimei.util;

import java.io.InputStream;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;

/**
 * @author shichen.feng@renren-inc.com
 */
public class HttpUtil {

    public static String get(String url) {
        try {
            HttpClient client = new HttpClient();
            GetMethod get = new GetMethod(url);
            client.executeMethod(get);
            if (get.getStatusCode() == HttpStatus.SC_OK)
                return get.getResponseBodyAsString();
            else
                return null;
        } catch (Exception e) {
            return null;
        }
    }

    public static byte[] getByte(String url) {
        try {
            HttpClient client = new HttpClient();
            GetMethod get = new GetMethod(url);
            client.executeMethod(get);
            if (get.getStatusCode() == HttpStatus.SC_OK)
                return get.getResponseBody();
            else
                return null;
        } catch (Exception e) {
            return null;
        }
    }

    public static InputStream getStream(String url) {
        try {
            HttpClient client = new HttpClient();
            GetMethod get = new GetMethod(url);
            client.executeMethod(get);
            if (get.getStatusCode() == HttpStatus.SC_OK)
                return get.getResponseBodyAsStream();
            else
                return null;
        } catch (Exception e) {
            return null;
        }
    }
}
